﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class product : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            getProductByID();
            UpdateViewed();
            getReviews();
        }

        
    }
    public void getProductByID()
    {
        productBus probus = new productBus();
        DataTable product = probus.getProductByID(Convert.ToInt32(Request.QueryString["id"].ToString()));
        productdetail.DataSource = product;
        productdetail.DataBind();
        product_des.DataSource = product;
        product_des.DataBind();
        getProductRelated(Convert.ToInt32(Request.QueryString["id"].ToString()));
    }
    public void getReviews(){
        reviewBUS rebus = new reviewBUS();
        DataTable dt = rebus.getReviewsByStatus(true);
        DataTable data = new DataTable();
        data.Columns.Add("Customer_name");
        data.Columns.Add("subject");
        data.Columns.Add("comment");
        data.Columns.Add("date");
        foreach (DataRow re in dt.Rows)
        {
            
            DataRow dr = data.NewRow();
            dr["Customer_name"] = getCustomerNameByID(Convert.ToInt32(re["customer_id"]));
            dr["subject"] = re["subject"].ToString();
            dr["comment"] = re["comment"].ToString();
            dr["date"] = Convert.ToDateTime(re["date_modified"]);
            data.Rows.Add(dr);
        }
        

        listReview.DataSource = data;
        listReview.DataBind();
    }
    public void UpdateViewed(){
        productBus probus = new productBus();
        probus.UpdateViewedByProdutID(Convert.ToInt32(Request.QueryString["id"].ToString()));
    }
    public void getProductRelated(int product_id)
    {
        productBus probus = new productBus();
        DataTable productrelease = probus.getProductRelatedByID(product_id);
        dtlRelated.DataSource = productrelease;
        dtlRelated.DataBind();
    }
    public void getProductOption(){
        optionBus optionBus = new optionBus();
        DataTable product_option = optionBus.getProductOptionByProductId(Convert.ToInt32(Request.QueryString["id"].ToString()));
    }

    protected void buy_Click(object sender, EventArgs e)
    {
        Label id=productdetail.FindControl("id_product") as Label;
        TextBox tb_quantity = productdetail.FindControl("text_qt") as TextBox;
        cartBus.carts.AddCart(Convert.ToInt32(id.Text.ToString()), Convert.ToInt32(tb_quantity.Text.ToString()));
        getProductByID();
        Response.Redirect("default.aspx?action=cart_home");
    }
    protected void submitReview_Click(object sender, EventArgs e)
    {
        reviewBUS reviewbus = new reviewBUS();
        if(Session["username"]!=null){
            int product_id = Convert.ToInt32(Request.QueryString["id"].ToString());
            int customer_id = this.getCustomerID(Session["username"].ToString());
            string subject = subjectReview.Text.ToString();
            string comment = commentReview.Text.ToString();
            review re = new review(product_id, customer_id, subject, comment, false);
            if(reviewbus.InsertReview(re)){
                ScriptManager.RegisterStartupScript(this, typeof(Page), "Test.com", "<script>alert('Ý kiến của bạn đã được gửi!Admin sẽ phê duyệt ý kiến của bạn')</script>", false);
            }else{
                ScriptManager.RegisterStartupScript(this, typeof(Page), "Test.com", "<script>alert('Ý kiến của bạn bị lỗi')</script>", false);
            }
        }else{
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Test.com", "<script>alert('Thành viên mới được phép gửi ý kiến!')</script>", false);
        }
        
        
    }
    public int getCustomerID(string username){
        CustomerBus cusBus = new CustomerBus();
        Customer cus = cusBus.getCustomerByEmail(username);
        return Convert.ToInt32(cus.Customer_id);
    }
    public string getCustomerNameByID(int customer_id){
        CustomerBus cusBus = new CustomerBus();
        Customer cus = cusBus.getCustomerById(customer_id);
        return cus.First_name;
    }
}